﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text.RegularExpressions;

public partial class cms_cmsGeneral : System.Web.UI.Page
{
    private const string IMAGELOGO = "its-logo.png";
    private const string ICONIPHONE = "iPhone-Footer.png";
    private const string ICONIPHONEHOVER = "iPhone-Footer-h.png";
    private const string ICONANDROID = "Android-Footer.png";
    private const string ICONANDROIDHOVER = "Android-Footer-h.png";
    private const string ICONBLACKBERRY = "BB-Footer.png";
    private const string ICONBLACKBERRYHOVER = "BB-Footer-h.png";
    private const string ICONWINDOWS = "WindowsPhone-Footer.png";
    private const string ICONWINDOWSHOVER = "WindowsPhone-Footer-h.png";
    private const string ICONIPAD = "iPad-Footer.png";
    private const string ICONIPADHOVER = "iPad-Footer-h.png";
    private string BackgroundColor = "";
    private string Color = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadImage();
            ReadCssColorPage();
        }
        ReadCssColorPage();
    }

    private void LoadImage()
    {
        if (File.Exists(Server.MapPath("~/images/" + IMAGELOGO)))
        {
            imgLogo.ImageUrl = "~/images/" + IMAGELOGO;
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/" + IMAGELOGO));
            lblSizeLogo.Text = "(H x W:" + img.Height + " x " + img.Width + " )";
        }

        if (File.Exists(Server.MapPath("~/images/" + ICONIPHONE)))
        {
            imgIconIphone.ImageUrl = "~/images/" + ICONIPHONE;
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/" + ICONIPHONE));
            lblSizeIconIphone.Text = "(H x W:" + img.Height + " x " + img.Width + " )";
        }

        if (File.Exists(Server.MapPath("~/images/" + ICONIPHONEHOVER)))
        {
            imgIconIphoneHover.ImageUrl = "~/images/" + ICONIPHONEHOVER;
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/" + ICONIPHONEHOVER));
            lblSizeIconIphoneHover.Text = "(H x W:" + img.Height + " x " + img.Width + " )";
        }

        if (File.Exists(Server.MapPath("~/images/" + ICONANDROID)))
        {
            imgIconAndroid.ImageUrl = "~/images/" + ICONANDROID;
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/" + ICONANDROID));
            lblSizeIconAndroid.Text = "(H x W:" + img.Height + " x " + img.Width + " )";
        }

        if (File.Exists(Server.MapPath("~/images/" + ICONANDROIDHOVER)))
        {
            imgIconAndroidHover.ImageUrl = "~/images/" + ICONANDROIDHOVER;
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/" + ICONANDROIDHOVER));
            lblSizeIconAndroidHover.Text = "(H x W:" + img.Height + " x " + img.Width + " )";
        }

        if (File.Exists(Server.MapPath("~/images/" + ICONBLACKBERRY)))
        {
            imgIconBlackBerry.ImageUrl = "~/images/" + ICONBLACKBERRY;
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/" + ICONBLACKBERRY));
            lblSizeIconBlackBerry.Text = "(H x W:" + img.Height + " x " + img.Width + " )";
        }

        if (File.Exists(Server.MapPath("~/images/" + ICONBLACKBERRYHOVER)))
        {
            imgIconBlackBerryHover.ImageUrl = "~/images/" + ICONBLACKBERRYHOVER;
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/" + ICONBLACKBERRYHOVER));
            lblSizeIconBlackBerryHover.Text = "(H x W:" + img.Height + " x " + img.Width + " )";
        }

        if (File.Exists(Server.MapPath("~/images/" + ICONWINDOWS)))
        {
            imgIconWindows.ImageUrl = "~/images/" + ICONWINDOWS;
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/" + ICONWINDOWS));
            lblSizeIconWindows.Text = "(H x W:" + img.Height + " x " + img.Width + " )";
        }

        if (File.Exists(Server.MapPath("~/images/" + ICONWINDOWSHOVER)))
        {
            imgIconWindowsHover.ImageUrl = "~/images/" + ICONWINDOWSHOVER;
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/" + ICONWINDOWSHOVER));
            lblSizeIconWindowsHover.Text = "(H x W:" + img.Height + " x " + img.Width + " )";
        }

        if (File.Exists(Server.MapPath("~/images/" + ICONIPAD)))
        {
            imgIconIpad.ImageUrl = "~/images/" + ICONIPAD;
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/" + ICONIPAD));
            lblSizeIconIpad.Text = "(H x W:" + img.Height + " x " + img.Width + " )";
        }

        if (File.Exists(Server.MapPath("~/images/" + ICONIPADHOVER)))
        {
            imgIconIpadHover.ImageUrl = "~/images/" + ICONIPADHOVER;
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/" + ICONIPADHOVER));
            lblSizeIconIpadHover.Text = "(H x W:" + img.Height + " x " + img.Width + " )";
        }
    }

    private string CleanUp(string s)
    {
        string temp = s;
        string reg = "(/\\*(.|[\r\n])*?\\*/)|(//.*)";
        Regex r = new Regex(reg);
        temp = r.Replace(temp, "");
        temp = temp.Replace("\r", "").Replace("\n", "");
        return temp;
    }

    private void ReadCssColorPage()
    {
        if (File.Exists(Server.MapPath("~/css/Site.css")))
        {
            string a = File.ReadAllText(Server.MapPath("~/css/Site.css"));
            if (!string.IsNullOrEmpty(a))
            {
                string content = CleanUp(a);
                string[] parts = content.Split('}');
                if (parts.Length > 0)
                {
                    string part = parts[0];
                    part = part.Remove(0, 5);
                    string[] tags = part.Split(';');
                    if (tags.Length > 5)
                    {
                        BackgroundColor = tags[3].Trim();
                        Color = tags[4];
                    }
                }
            }
        }
    }

    public void EditorialResponse(string fileName)
    {
        string directory = Server.MapPath("~/css/");
        List<string> list = new List<string>();
        using (StreamReader reader = new StreamReader(directory + fileName))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                list.Add(line);
            }
            reader.Close();
        }
        if (list.Count > 10)
        {
            list[6] = "background-color: #" + txtBackgroundColorSite.Text + ";";
        }

        StreamWriter writer = new StreamWriter(directory + fileName);

        foreach (string listItem in list)
        {
            writer.WriteLine(listItem);
        }

        writer.Close();
    }

    public void EditorialResponseTextColor(string fileName)
    {
        string directory = Server.MapPath("~/css/");
        List<string> list = new List<string>();
        using (StreamReader reader = new StreamReader(directory + fileName))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                list.Add(line);
            }
            reader.Close();
        }
        if (list.Count > 10)
        {
            list[7] = "Color: #" + txtTextColor.Text + ";";
        }

        StreamWriter writer = new StreamWriter(directory + fileName);

        foreach (string listItem in list)
        {
            writer.WriteLine(listItem);
        }

        writer.Close();
    }

    public void EditorialResponseTitleColorHome(string fileName)
    {
        string directory = Server.MapPath("~/css/");
        List<string> list = new List<string>();
        using (StreamReader reader = new StreamReader(directory + fileName))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                list.Add(line);
            }
            reader.Close();
        }
        if (list.Count > 10)
        {
            list[92] = "Color: #" + txtTitleColor.Text + ";";
        }

        StreamWriter writer = new StreamWriter(directory + fileName);

        foreach (string listItem in list)
        {
            writer.WriteLine(listItem);
        }

        writer.Close();
    }

    public void EditorialResponseTitleColorAppDevelopment(string fileName)
    {
        string directory = Server.MapPath("~/css/");
        List<string> list = new List<string>();
        using (StreamReader reader = new StreamReader(directory + fileName))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                list.Add(line);
            }
            reader.Close();
        }
        if (list.Count > 10)
        {
            list[89] = "Color: #" + txtTitleColor.Text + ";";
        }

        StreamWriter writer = new StreamWriter(directory + fileName);

        foreach (string listItem in list)
        {
            writer.WriteLine(listItem);
        }

        writer.Close();
    }

    private void SaveBackgroundColorPage()
    {
        if (File.Exists(Server.MapPath("~/css/Site.css")))
        {
            StreamReader stream = new StreamReader(Server.MapPath("~/css/Site.css"));
            if (stream!=null)
            {
                string line;
                while ((line = stream.ReadLine()) != null)
                {

                }
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (FileUploadLogo.HasFile && Utils.IsImageFile(FileUploadLogo.FileName))
        {
            FileUploadLogo.SaveAs(Server.MapPath("~/images/" + IMAGELOGO));
        }
        if (FileUploadIconIphone.HasFile && Utils.IsImageFile(FileUploadIconIphone.FileName))
        {
            FileUploadIconIphone.SaveAs(Server.MapPath("~/images/" + ICONIPHONE));
        }
        if (FileUploadIconIphoneHover.HasFile && Utils.IsImageFile(FileUploadIconIphoneHover.FileName))
        {
            FileUploadIconIphoneHover.SaveAs(Server.MapPath("~/images/" + ICONIPHONEHOVER));
        }
        if (FileUploadIconAndroid.HasFile && Utils.IsImageFile(FileUploadIconAndroid.FileName))
        {
            FileUploadIconAndroid.SaveAs(Server.MapPath("~/images/" + ICONANDROID));
        }
        if (FileUploadIconAndroidHover.HasFile && Utils.IsImageFile(FileUploadIconAndroidHover.FileName))
        {
            FileUploadIconAndroidHover.SaveAs(Server.MapPath("~/images/" + ICONANDROIDHOVER));
        }
        if (FileUploadIconBlackBerry.HasFile && Utils.IsImageFile(FileUploadIconBlackBerry.FileName))
        {
            FileUploadIconBlackBerry.SaveAs(Server.MapPath("~/images/" + ICONBLACKBERRY));
        }
        if (FileUploadIconBlackBerryHover.HasFile && Utils.IsImageFile(FileUploadIconBlackBerryHover.FileName))
        {
            FileUploadIconBlackBerryHover.SaveAs(Server.MapPath("~/images/" + ICONBLACKBERRYHOVER));
        }
        if (FileUploadIconWindows.HasFile && Utils.IsImageFile(FileUploadIconWindows.FileName))
        {
            FileUploadIconWindows.SaveAs(Server.MapPath("~/images/" + ICONWINDOWS));
        }
        if (FileUploadIconWindowsHover.HasFile && Utils.IsImageFile(FileUploadIconWindowsHover.FileName))
        {
            FileUploadIconWindowsHover.SaveAs(Server.MapPath("~/images/" + ICONWINDOWSHOVER));
        }
        if (FileUploadIconIpad.HasFile && Utils.IsImageFile(FileUploadIconIpad.FileName))
        {
            FileUploadIconIpad.SaveAs(Server.MapPath("~/images/" + ICONIPAD));
        }
        if (FileUploadIconIpadHover.HasFile && Utils.IsImageFile(FileUploadIconIpadHover.FileName))
        {
            FileUploadIconIpadHover.SaveAs(Server.MapPath("~/images/" + ICONIPADHOVER));
        }

        if (!string.IsNullOrEmpty(txtBackgroundColorSite.Text))
        {
            EditorialResponse("Site.css");
        }
        if (!string.IsNullOrEmpty(txtTextColor.Text))
        {
            EditorialResponseTextColor("Site.css");
        }

        if (!string.IsNullOrEmpty(txtTitleColor.Text))
        {
            EditorialResponseTitleColorHome("Home.css");
            EditorialResponseTitleColorHome("IPhoneApplicationDevelopment.css");
        }
    }
}